﻿/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

/// <reference path="../base.js" />
/// <reference path="../Libs/Mootools-Core.js" />
/// <reference path="../Libs/Mootools-More.js" />
/// <reference path="../Libs/Clientcide.js" />

UIWindowDefaultOptions = {
  caption: "",
  content: "",
  name: "a UIWindow",
  modal: true,
  closeButton: true
};

var UIWindowZIndex = 50;

/*
Class: UIWindow
Simple DHTML window class.
*/
var UIWindow = new Class({
  /*
  Hash: options
  Option for this UIWindow. Can include:
  - caption (String)
  - content (String)
  - name (String)
  - modal (bool)
  - closeButton (bool)
  */
  options: {
    caption: "",
    content: "",
    name: null,
    modal: true,
    closeButton: true
  },
  /*
  Constructor: UIWindow
  Initializes the UI Window and the options supplied.
  */
  initialize: function(options)
  {
    this.setOptions(options);
    this.on = false;


    var html = "<table cellspacing='0' cellpadding='0' border='0'>"
           + "<tr style='height: 52px;'>"
           + "<td style='background-image: url(\"Images/Window/Top-Left.png\"); width: 24px;'>"
           + "</td>"
           + "<td style='background-image: url(\"Images/Window/Top.png\"); position: relative;' id='" + this.name + "Top'>"
           + "<span style='top: -4px; left: 0px; position: relative; font-family: Calibri, Verdana, Arial; font-size: 16px;' id='" + this.name + "Caption'>"
           + this.options.caption
           + "</span>"
           + "</td>"
           + "<td style='background-image: url(\"Images/Window/Top-Right.png\"); width: 24px;'>"
           + (this.options.closeButton ? "<a href='javascript:$(\"" + this.options.name + "\").retrieve(\"UIWindow\").hide()'><img src='Images/Close Button x16.png' alt='' style='position: relative; top: -4px; left: -7px; border: 0px;' /></a>" : "")
           + "</td>"
           + "</tr>"
           + "<tr>"
           + "<td style='background-image: url(\"Images/Window/Left.png\"); width: 24px;'>"
           + "</td>"
           + "<td style='background-image: url(\"Images/Window/Middle.png\");'>"
           + "<span style='font-family: Calibri, Verdana, Arial; text-align: center;' id='" + this.options.name + "Content'>"
           + this.options.content
           + "</span>"
           + "</td>"
           + "<td style='background-image: url(\"Images/Window/Right.png\"); width: 24px;'>"
           + "</td>"
           + "</tr>"
           + "<tr style='height: 24px;'>"
           + "<td style='background-image: url(\"Images/Window/Bottom-Left.png\"); width: 24px;'>"
           + "</td>"
           + "<td style='background-image: url(\"Images/Window/Bottom.png\");'>"
           + "</td>"
           + "<td style='background-image: url(\"Images/Window/Bottom-Right.png\"); width: 24px;'>"
           + "</td>"
           + "</tr>"
           + "</table>";

    if (this.options.modal)
      this.modalizer = new Modalizer();

    $(document.body).adopt(new Element("div", { html: html, id: this.options.name }));
    $(this.options.name).hide();
    $(this.options.name).store("UIWindow", this);
    //  $(this.name + "Top").makeDraggable();
  },

  /*
  Function: show
  Displays the window.
  */
  show: function()
  {
    if (this.options.modal)
      this.modalizer.modalShow({ hideOnClick: false, modalStyle: { "z-index": UIWindowZIndex }, layerId: this.options.name + "Modal" });

    $(this.options.name).position();
    $(this.options.name).show();
    $(this.options.name).fade("in");
    $(this.options.name).setStyle("z-index", UIWindowZIndex + 1);
    $(this.options.name).pin();
    UIWindowZIndex += 10;

    this.on = true;
  },

  /*
  Function: hide
  Hides the window.
  */
  hide: function()
  {
    $(this.options.name).fade("out").get("tween").addEvent("complete", (function()
    {
      if (this.parent.on)
        return;

      $(this.parent.options.name).hide();

      if (this.parent.options.modal)
        this.parent.modalizer.modalHide();
    }).bind({ parent: this }));

    UIWindowZIndex -= 10;
    this.on = false;
  },

  /*
  Function: set
  Sets either the Caption or the Content of the window.
  */
  set: function(key, value)
  {
    switch (key)
    {
      case "caption":
        $(this.options.name + "Caption").set("html", value);
        break;

      case "content":
        $(this.options.name + "Content").set("html", value);
        break;

      default:
        throw "Unknown key.";
    }
  }
});
UIWindow.implement(new Options);